package cn.com.haopy.yue.ai.admin.web;

import cn.com.haopy.yue.ai.admin.annotation.RequiresPermissionsDesc;
import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskTopicOptions;
import cn.com.haopy.yue.ai.db.entity.arisk.HOSAdmin;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskTopicOptionsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Description: TODO
 * @Author DuTao
 * @Date 2025/3/26 14:13
 */
@RestController
@RequestMapping("/admin/risk/topic/options")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "AI题目答案相关")
public class AdminAiRiskTopicOptionsController {
    @Autowired
    private AIRiskTopicOptionsService riskTopicOptionsService;

    @RequiresPermissions("admin:risk:topic:options:list")
    @RequiresPermissionsDesc(menu = {"信息维护", "AI题目答案管理"}, button = "AI题目答案查询")
    @GetMapping("/list")
    @ApiOperation("列表")
    public Object list(@RequestParam(required = false) @ApiParam("题目id") Integer topicId) {
        List<AIRiskTopicOptions> riskTopicList = riskTopicOptionsService.listByTopicId(topicId);
        return ResponseUtil.ok(riskTopicList);
    }

    @RequiresPermissions("admin:risk:topic:options:create")
    @RequiresPermissionsDesc(menu = {"信息维护", "AI题目答案管理"}, button = "AI题目答案添加")
    @PostMapping("/create")
    @ApiOperation("新增")
    @Transactional
    public Object create(@RequestBody AIRiskTopicOptions riskTopicOptions) {
        Object error = validate(riskTopicOptions);
        if (error != null) {
            return error;
        }
        Subject currentUser = SecurityUtils.getSubject();
        HOSAdmin admin = (HOSAdmin) currentUser.getPrincipal();
        riskTopicOptions.setCreateBy(admin.getUsername());
        riskTopicOptions.setUpdateBy(admin.getUsername());
        List<AIRiskTopicOptions> riskTopicList = riskTopicOptionsService.getByCondition(riskTopicOptions.getTopicId(), riskTopicOptions.getOptionName());
        if (!CollectionUtils.isEmpty(riskTopicList)) {
            return ResponseUtil.fail("题目答案已经存在");
        }
        boolean isSuccess = riskTopicOptionsService.save(riskTopicOptions);
        if (isSuccess) {
            return ResponseUtil.ok("添加成功");
            /**
             riskTopicOptions.setOptionValue(riskTopicOptions.getId().toString());
             if (!riskTopicOptionsService.updateById(riskTopicOptions)) {
             return ResponseUtil.fail("添加失败");
             }**/
        }
        return ResponseUtil.fail("添加失败");
    }

    @RequiresPermissions("admin:risk:topic:options:update")
    @RequiresPermissionsDesc(menu = {"信息维护", "AI题目管理"}, button = "AI题目答案修改")
    @PostMapping("/update")
    @ApiOperation("编辑")
    @Transactional
    public Object update(@RequestBody AIRiskTopicOptions riskTopic) {
        AIRiskTopicOptions riskTopicEntity = riskTopicOptionsService.getById(riskTopic.getId());
        if (Objects.isNull(riskTopicEntity)) {
            return ResponseUtil.fail("答案信息不存在");
        }

        // 题目答案唯一性校验
        if (!Objects.equals(riskTopic.getOptionName(), riskTopicEntity.getOptionName())) {
            List<AIRiskTopicOptions> riskTopicList = riskTopicOptionsService.getByCondition(riskTopic.getTopicId(), riskTopic.getOptionName());
            if (!CollectionUtils.isEmpty(riskTopicList)) {
                return ResponseUtil.fail("答案已经存在！");
            }
        }
        Subject currentUser = SecurityUtils.getSubject();
        HOSAdmin admin = (HOSAdmin) currentUser.getPrincipal();
        riskTopic.setUpdateBy(admin.getUsername());
        if (riskTopicOptionsService.updateById(riskTopic)) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }

    @RequiresPermissions("admin:risk:topic:options:delete")
    @RequiresPermissionsDesc(menu = {"信息维护", "AI题目管理"}, button = "AI题目答案删除")
    @GetMapping("/delete")
    @ApiOperation("删除")
    @Transactional
    public Object delete(@RequestParam Integer id) {
        if (riskTopicOptionsService.removeById(id)) {
            return ResponseUtil.ok("删除成功");
        }
        return ResponseUtil.fail("删除数据失败");
    }

    private Object validate(AIRiskTopicOptions data) {
        if (Objects.isNull(data.getTopicId())) {
            return ResponseUtil.badArgumentValue();
        }
        if (!StringUtils.hasText(data.getOptionName())) {
            return ResponseUtil.badArgumentValue();
        }
        return null;
    }
}
